package com.linkpow.smartelevator.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.SerializationFeature;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName JsonUtil
 * @Description Json数据转化
 * @Author 王丹
 * @Date 2019/11/25 14:25
 * @Version 1.0
 */
@Slf4j
public class JsonUtil {

    private static final ObjectMapper mapper;

    static {
        mapper = new ObjectMapper();
        //SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //mapper.setDateFormat(fmt);
        //mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);//单引号
    }

    /**
     * 把java对象转换成json字符串
     * @param obj 转换对象
     * @return json字符串
     * @throws JsonProcessingException
     */
    public static String toJson(Object obj) throws JsonProcessingException {

        String json = "";
        try {
            json = mapper.writeValueAsString(obj);
            return json;
        } catch (JsonProcessingException e) {
            log.error("json转换异常", e);
            throw e;
        }
    }

    /**
     * 把json字符串转换成java对象
     * @param jsonStr json字符串
     * @param clazz 转换后的对象
     * @return java对象
     * @throws IOException
     */
    public static <T> T fromJson(String jsonStr, Class<T> clazz) throws IOException {

        try {
            return mapper.readValue(jsonStr, clazz);
        } catch (IOException e) {
            log.error("json转换异常", e);
            throw e;
        }
    }

    public static <T> T fromJson(String jsonStr, TypeReference<T> type) throws IOException {

        try {
            return mapper.readValue(jsonStr, type);
        } catch (IOException e) {
            log.error("json转换异常", e);
            throw e;
        }
    }

    public static Map<String, Object> json2map(String jsonStr) throws IOException{
        return mapper.readValue(jsonStr, Map.class);
    }

    public static <T> Map<String, T> json2map(String jsonStr, Class<T> clazz) throws IOException {
        Map<String, Map<String, Object>> map = mapper.readValue(jsonStr, new TypeReference<Map<String, Map<String, Object>>>(){});
        Map<String, T> result = new HashMap<>();
        map.forEach((String k, Map v) -> result.put(k, map2pojo(v, clazz)));
        return result;
    }

    /**
     * 把json字符串转换成List
     * @param jsonStr json字符串
     * @param clazz 转换后的对象
     * @return java对象
     * @throws IOException
     */
    public static <T> List<T> json2list(String jsonStr, Class<T> clazz) throws IOException {

        try {
            List<Map<String, Object>> list = mapper.readValue(jsonStr, new TypeReference<List<Map<String, Object>>>(){});
            List<T> result = new ArrayList<>();
            list.forEach(map -> result.add(map2pojo(map, clazz)));
            return result;
        } catch (IOException e) {
            log.error("json转换异常", e);
            throw e;
        }
    }

    private static <T> T map2pojo(Map map, Class<T> clazz){
        return mapper.convertValue(map, clazz);
    }

}
